//
//  XDAllScenicViewController.m
//  Le way walkers
//
//  Created by YaZhiLiang on 15/5/27.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDAllScenicViewController.h"

@interface XDAllScenicViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation XDAllScenicViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
    }
    
    return self;
}


- (void)createSubTableView
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    self.mytabeView =[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    self.mytabeView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.mytabeView.dataSource = self;
    
    self.mytabeView.delegate = self;
    
    self.mytabeView.contentInset = UIEdgeInsetsMake(0, 0, 113, 0);
    
    [self.view addSubview:self.mytabeView];
    
    [_mytabeView release];
    
}

- (void)back:(UIBarButtonItem *)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData
{
    
    NSLog(@"----------- %@, ---------- %@", self.type, self.ScenicID);
    
    if (self.num >= 1) {
        
        self.numinfo = self.num * 20;
    }
    
        [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/cities/?start=%ld", self.type, self.ScenicID, self.numinfo] Cookie:@" bt_devid=i_70977b3f76167894392f3ada99cb3f82fb57705ae698d0fc08ca99fd7f26cdf9057e84df; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1431763985,1431910698,1432552570,1432727789; sessionid=75f121c0f7165c1e3eef07a24359f126" completion:^(id result) {
            
            NSArray *arrr = [result objectForKey:@"items"];
            
            for (NSDictionary *dic in arrr) {
                
                XDPlaceAllScenic *xdPlaceAll = [[XDPlaceAllScenic alloc]initWithDic:dic];
                
                [self.Arr addObject:xdPlaceAll];
                
                
                [xdPlaceAll release];
            }
            
            [self.mytabeView reloadData];
            
            [self.gifImageView stopAnimating];
            
            self.gifView.hidden = YES;
            
        } error:^(id error1) {
           
        [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=%ld&latitude=38.883756&longitude=121.545113&sign=ced1b747e6b26f477f5b5d151eea41f9", self.type, self.ScenicID, self.numinfo] Cookie:@"bt_devid=i_70977b3f76167894392f3ada99cb3f82fb57705ae698d0fc08ca99fd7f26cdf9057e84df; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1431763985,1431910698,1432552570,1432727789; sessionid=75f121c0f7165c1e3eef07a24359f126" completion:^(id result) {
            
            
            NSArray *arrr = [result objectForKey:@"items"];
            
            for (NSDictionary *dic in arrr) {
                
                XDPlaceAllScenic *xdPlaceAll = [[XDPlaceAllScenic alloc]initWithDic:dic];
                
                [self.Arr addObject:xdPlaceAll];
                
                
                [xdPlaceAll release];
            }
            
            [self.mytabeView reloadData];
            [self.gifImageView stopAnimating];
            
            self.gifView.hidden = YES;
            
//             [self.mytabeView footerEndRefreshing];
        }];
            
            [self.mytabeView reloadData];
            [self.gifImageView stopAnimating];
            
            self.gifView.hidden = YES;
        }];
            
    
    self.num++;
                 [self.mytabeView footerEndRefreshing];
        
//    }
//    if ([self.type integerValue] == 2 || [self.type integerValue] == 3) {
//        
//        [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=0&latitude=38.883589&longitude=121.545271&sign=d22aa0310a146663d9ec9dcbc8cdf0b9", self.type, self.ScenicID] Cookie:@"bt_devid=i_70977b3f76167894392f3ada99cb3f82fb57705ae698d0fc08ca99fd7f26cdf9057e84df; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1431674200,1431763985,1431910698,1432552570; sessionid=75f121c0f7165c1e3eef07a24359f126" completion:^(id result) {
//            
//           
//            NSArray *arrr = [result objectForKey:@"items"];
//            
//            for (NSDictionary *dic in arrr) {
//                
//                XDPlaceAllScenic *xdPlaceAll = [[XDPlaceAllScenic alloc]initWithDic:dic];
//                
//                [self.Arr addObject:xdPlaceAll];
//                
//                
//                [xdPlaceAll release];
//            }
//            
//            
//            [self.mytabeView reloadData];
//            
//        }];
//        
//            [self.mytabeView footerEndRefreshing];
//    }

    
}

- (void)yangTuo
{
    [self getData];
    
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"-------- ### %ld", self.Arr.count);
    
    return self.Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XDAllScenicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celldentifier"];
    
    if (!cell) {
        
        cell = [[XDAllScenicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celldentifier"];
        
    }
    
    XDPlaceAllScenic *xdplace = self.Arr[indexPath.row];
    
    cell.xdplaceAll = xdplace;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    XDPlaceScenicDetailsViewController *scenicVC = [[XDPlaceScenicDetailsViewController alloc]init];
    XDAllScenicTableViewCell * cell = (XDAllScenicTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    XDPlaceCityViewController *cityVC = [[XDPlaceCityViewController alloc]init];
    
    scenicVC.typeName = cell.type;
    
    scenicVC.ScenicID = cell.ScenicID;
    
    cityVC.ScenicID = cell.ScenicID;
    
    cityVC.typeName = cell.type;
    
    if ([scenicVC.typeName integerValue] == 1 || [scenicVC.typeName integerValue] == 2 || [scenicVC.typeName integerValue] == 3) {
        
        [self.navigationController pushViewController:scenicVC animated:YES];
        
    }
    
    if ([scenicVC.typeName integerValue] == 5) {
        
        [self.navigationController pushViewController:cityVC animated:YES];
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.Arr = [NSMutableArray array];
    
    [self createSubTableView];
    
    [self yangTuo];
    
    [self.mytabeView addFooterWithCallback:^{
       
        [self getData];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
